package com.pactera.madp.cp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.cp.api.dto.marketing.MarketingClassificationCreateDTO;
import com.pactera.madp.cp.api.dto.marketing.MarketingClassificationUpdateDTO;
import com.pactera.madp.cp.api.entity.marketing.*;
import com.pactera.madp.cp.api.vo.marketing.MarketingClassificationVO;
import com.pactera.madp.cp.handler.CorpContextHolder;
import com.pactera.madp.cp.mapper.marketing.MarketingClassificationMapper;
import com.pactera.madp.cp.service.*;
import lombok.Data;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Data
public class MarketingClassificationServiceImpl  extends ServiceImpl<MarketingClassificationMapper, MarketingClassificationEntity> implements MarketingClassificationService {

    private final MarketingArticleService marketingArticleService;
    private final MarketingLinkService marketingLinkService;
    private final MarketingPdfService marketingPdfService;
    private final MarketingPptService marketingPptService;
    private final MarketingWordService marketingWordService;
    private final MarketingPosterService marketingPosterService;
    private final MarketingExcelService marketingExcelService;

    @Override
    public boolean insert(MarketingClassificationCreateDTO dto) {
        int corpId = CorpContextHolder.getCorpId();
        int tenantId = AccountService.getTenantId();
        MarketingClassificationEntity marketingClassificationEntity = new MarketingClassificationEntity();
        BeanUtil.copyProperties(dto,marketingClassificationEntity);
        marketingClassificationEntity.setCorpId(corpId);
        marketingClassificationEntity.setTenantId(tenantId);
        return save(marketingClassificationEntity);
    }

    @Override
    public boolean put(MarketingClassificationUpdateDTO dto) {
        MarketingClassificationEntity marketingClassificationEntity = new MarketingClassificationEntity();
        BeanUtil.copyProperties(dto,marketingClassificationEntity);
        return update(marketingClassificationEntity,Wrappers.<MarketingClassificationEntity>lambdaQuery().eq(MarketingClassificationEntity::getId,dto.getId()));
    }

    @Override
    public R removeById(Integer id) {
        Integer corpId = CorpContextHolder.getCorpId();
        if (!marketingArticleService.list(Wrappers.<MarketingArticleEntity>lambdaQuery().eq(MarketingArticleEntity::getClassification,id).eq(MarketingArticleEntity::getCorpId,corpId)).isEmpty()){
            return R.failed("该分类已有素材存在,无法删除！");
        }
        if(!marketingLinkService.list(Wrappers.<MarketingLinkEntity>lambdaQuery().eq(MarketingLinkEntity::getClassification,id).eq(MarketingLinkEntity::getCorpId,corpId)).isEmpty()){
            return R.failed("该分类已有素材存在,无法删除！");
        }
        if(!marketingPosterService.list(Wrappers.<MarketingPosterEntity>lambdaQuery().eq(MarketingPosterEntity::getClassification,id).eq(MarketingPosterEntity::getCorpId,corpId)).isEmpty()){
            return R.failed("该分类已有素材存在,无法删除！");
        }
        if(!marketingPdfService.list(Wrappers.<MarketingPdfEntity>lambdaQuery().eq(MarketingPdfEntity::getClassification,id).eq(MarketingPdfEntity::getCorpId,corpId)).isEmpty()){
            return R.failed("该分类已有素材存在,无法删除！");
        }
        if(!marketingPptService.list(Wrappers.<MarketingPptEntity>lambdaQuery().eq(MarketingPptEntity::getClassification,id).eq(MarketingPptEntity::getCorpId,corpId)).isEmpty()){
            return R.failed("该分类已有素材存在,无法删除！");
        }
        if(!marketingWordService.list(Wrappers.<MarketingWordEntity>lambdaQuery().eq(MarketingWordEntity::getClassification,id).eq(MarketingWordEntity::getCorpId,corpId)).isEmpty()){
            return R.failed("该分类已有素材存在,无法删除！");
        }
        if(!marketingExcelService.list(Wrappers.<MarketingExcelEntity>lambdaQuery().eq(MarketingExcelEntity::getClassification,id).eq(MarketingExcelEntity::getCorpId,corpId)).isEmpty()) {
            return R.failed("该分类已有素材存在,无法删除！");
        }
        return remove(Wrappers.<MarketingClassificationEntity>lambdaQuery().eq(MarketingClassificationEntity::getId,id))?R.ok():R.failed("删除失败");
    }

    @Override
    public List<MarketingClassificationVO> listAll() {
        Integer corpId = CorpContextHolder.getCorpId();
        return  list(Wrappers.<MarketingClassificationEntity>lambdaQuery().eq(MarketingClassificationEntity::getCorpId,corpId)).stream().map(entity->{
            MarketingClassificationVO marketingClassificationVO = new MarketingClassificationVO();
            BeanUtil.copyProperties(entity,marketingClassificationVO);
            return marketingClassificationVO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<MarketingClassificationVO> listAllWechat() {
        Integer corpId = CorpContextHolder.getCorpId();
        return  list(Wrappers.<MarketingClassificationEntity>lambdaQuery().eq(MarketingClassificationEntity::getCorpId,corpId)).stream().map(entity->{
            MarketingClassificationVO marketingClassificationVO = new MarketingClassificationVO();
            BeanUtil.copyProperties(entity,marketingClassificationVO);
            return marketingClassificationVO;
        }).collect(Collectors.toList());
    }


}